<?php
/*
 * @Descripttion: 
 * @Author: Twoknives
 * @Date: 2021-07-16 21:26:57
 * @LastEditTime: 2021-12-04 23:52:14
 * @FilePath: \njz\app\admin\controller\manageuser\User.php
 */

namespace app\admin\controller\manageuser;

use app\admin\controller\anx\Adv;
use app\api\model\Agent;
use app\api\model\User as ModelUser;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="用户管理")
 */
class user extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\User();
        
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                foreach ($list as $k => $v){
                    //楼号
                    $user_building_number = Db::name("user_building_number")->where("id",$v["building_number_id"])->find();
                    if(!empty($user_building_number)){
                        $v['user_building_number'] = $user_building_number['title'];
                    }else{
                        $v['user_building_number'] = "";
                    }

                    //户型面积
                    $user_unitarea = Db::name("user_unitarea")->where("id",$v["unitarea_id"])->find();
                    if(!empty($user_unitarea)){
                        $v['user_unitarea'] = $user_unitarea['unit_type'].'/'.$user_unitarea['area'].'m²';
                    }else{
                        $v['user_unitarea'] = "";
                    }

                    if(!empty($v["building_number_id"]) && !empty($v["unitarea_id"])){
                        $v["is_check"] = true;
                    }else{
                        $v["is_check"] = false;
                    }

                   
                    $list[$k] = $v;
                }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function qwe(){
        $coupon = Db::name('coupon')->where('release_or_not',1)->order('id desc')->field('id,title as name,title as text')->select()->toArray();
        return json($coupon);
    }
 
   
    /**
    * @NodeAnotation(title="设置参团信息")
    */
    public function adopt_url()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            Db::name("user")->where('id', $post['id'])->update([
                "building_number_id"=>$post["building_number_id"],
                "unitarea_id"=>$post["unitarea_id"],
            ]);
            $this->success('操作成功', '');
        }else{
            $user_building_number = Db::name("user_building_number")->order("id desc")->select();
            $user_unitarea = Db::name("user_unitarea")->order("id desc")->select();
            $this->assign("user_building_number",$user_building_number);
            $this->assign("user_unitarea",$user_unitarea);
            return $this->fetch();
        }
      
    }

 
 

    
}